
var column = [];
var node;
function loadTable(){
    $("[ext-type='datagrid']").each(function (item) {
        var filter = $(this).attr("lay-filter");
        var col = $(this).attr("cols");
        var disabled = $(this).attr("disabled");
        // var page = $(this).attr("page");
        // var pageSize = $(this).attr("pageSize");
        // var limits = $(this).attr("limits");
        var cols = [];
        if(col != null && col != "" && col != undefined){
            var cols1 = JSON.parse(col);
            if(filter == "erpContractDetail"){
                $.each(cols1, function (i, item) {
                    if(item["field"] == "epId"){
                        item["templet"] = function(e){return e['epName']}
                    }
                    if(item["field"] == "units"){
                        item["templet"] = function(e){return getDictionary(77, e['units'])}
                    }
                    cols.push(item);
                })
            }
        }

        var data = $(this).attr("value");
        var td = [];
        if(data != null && data != "" && data != undefined){
            td = JSON.parse(data);
        }
        if(disabled != "disabled"){
            cols.push({field: 'op', title: '操作', minWidth: 120, toolbar: '#bar'});
        }

        table.render({
            elem: '#'+filter
            ,cols: [cols],
            data: td,
            done: function () {
                $("#"+filter).next('div').find("[data-field='op']").children().each(function (index) {
                    var dis = $("#"+filter).attr("disabled");
                    if($.trim($(this).text())=="操作" && dis != "disabled"){
                        $(this).html('<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" onclick="addTr(this, \''+filter+'\');">\n' +
                            '<i class="layui-icon">&#xe654;</i> 添加一行\n' +
                            '</button>')
                    }
                });
            }
        });

        table.on('tool('+ filter +')', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                var i = obj.tr.attr("data-index");
                layer.confirm('确定删除这行么', function(index){
                    obj.del();
                    var oldData =  table.cache[filter];
                    layer.close(index);
                });
            }
        });
    });
}


//添加一行
function addTr(e, filter) {
    node = $(e);
    var oldData =  table.cache[filter];
    var data = {}
    if(filter == "erpContractDetail"){
        window.frameElement.id="erpContractDetail";
        var frameId = window.frameElement && window.frameElement.id || '';
        parent.layer.open({
            type: 2
            ,title: '添加订单产品信息'
            ,area: ['900px', '600px']
            ,maxmin: false
            ,content: '/base/workFlow/workFlow/addOrderDetail.html?iframeId='+frameId
            ,zIndex: layer.zIndex
            ,end:function (data) {
                var json = node.attr("value-json");
                var product = JSON.parse(json.replace(/\'/g,'\"'));
                var no = oldData.length;
                product["no"] = no+1;
                oldData.push(product);
                table.reload(filter,{
                    data : oldData
                });

                var totalTaxTotal = 0;
                var untaxedUnitTotal = 0;
                $.each(oldData, function (index, itme) {
                    totalTaxTotal += Number(itme["totalTaxTotal"]);
                    untaxedUnitTotal += Number(itme["untaxedUnitTotal"]);
                });
                $("[name='totalTaxTotal']").val(totalTaxTotal.toFixed(2));
                $("[name='untaxedUnitTotal']").val(untaxedUnitTotal.toFixed(2));
            }
        });
    }else if(filter == "directCost"){
        //直接成本预算
    }else if(filter == "indirectCost"){
        //间接成本预算
    }else {

        oldData.push(data);

        table.reload(filter,{
            data : oldData
        });
    }
}


function getDictionary(val, d) {
    var v = ""
    $.ajax({
        async:false,
        type:"post",
        url:"/wf/dictionary/list",
        data:{"parentId":val},
        contentType:'application/x-www-form-urlencoded; charset=UTF-8',
        timeout: 60000,//超时时间设置为1分钟；
        success: function (data, textStatus) {
            if(data.code == "200"){
                $.each(data.rows, function (index, value) {
                    if(value.value == d){
                        v = value.cnName;
                    }
                });
            }
        },
        error: function (a, b, c) {
            // 请求出错处理,依然初始化咨询节点信息
            if (b == 'timeout') {
                layer.alert("请求超时，请再次尝试！");
            } else {
                layer.alert(url + "请求异常");
            }
        }
    });

    return v;
}